package com.hfxt.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.hfxt.entity.Provider;
import com.hfxt.entity.User;
import com.hfxt.util.ResultBaseUtil;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import com.hfxt.service.ProviderService;
import com.hfxt.util.PageUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@CrossOrigin
@RequestMapping("/provider")
public class ProviderController {
    //添加log4j日志
    private final Logger logger = Logger.getLogger(ProviderController.class);
    @Resource
    private ProviderService providerService;


    //供应商管理 条件分页
    @CrossOrigin
    @ResponseBody
    @RequestMapping("/providerList")
    public ResultBaseUtil providerList(String proName, Integer pageIndex, Integer pageSize) {
        System.out.println("proName------" + proName);
        System.out.println("pageIndex------" + pageIndex);
        System.out.println("pageSize------" + pageSize);
        Map<String, Object> map = new HashMap<>();
        if (pageIndex == null) {
            pageIndex = 1;
        }
        if (pageSize == null) {
            pageSize = 6;
        }
        PageInfo<Provider> pageInfo = providerService.queryAllProviderByPageInfo(proName,pageIndex, pageSize);
        System.out.println("pageInfo = " + pageInfo);
        map.put("providerList", pageInfo.getList());
        map.put("pageInfo", pageInfo);
        map.put("totalCount", pageInfo.getTotal());
        return ResultBaseUtil.ok("获取数据成功！").obj(map);
    }

    //查询供应商详细信息
    @RequestMapping("/view")
    public ModelAndView queryProviderById(Long id) {
        ModelAndView modelAndView = new ModelAndView();
        System.out.println("provider--id---" + id);
        Provider provider = providerService.queryProviderById(id);
        modelAndView.addObject("provider", provider);
        modelAndView.setViewName("providerView");
        return modelAndView;
    }

    //删除供应商
    @ResponseBody
    @RequestMapping("/deleteProviderById")
    public int deleteProviderById(Long id) {
        System.out.println("进入供应商删除方法！要删除的供应商的id=" + id);
        return providerService.deleteProviderById(id);
    }

    //加载供应商修改界面
    @RequestMapping("/toProviderUpdate")
    public String toProviderUpdate(Long id, Model model) {
        Provider provider = providerService.queryProviderById(id);
        System.out.println("需要修改的provider---" + provider);
        model.addAttribute("provider", provider);
        model.addAttribute("providerList", JSON.toJSONString(providerService.queryAllProviders()));
        return "providerUpdate";
    }

    //修改供应商信息
    @ResponseBody
    @RequestMapping("/providerUpdate")
    public int providerUpdate(Provider provider, HttpSession session) {
        User user = (User) session.getAttribute("user");
        System.out.println("修改页面传入的provider---" + provider);
        provider.setModifyBy(user.getId());
        provider.setModifyDate(new Date());
        return providerService.updateProvider(provider);
    }

    //加载供应商新增界面
    @RequestMapping("/toProviderAdd")
    public String toProviderAdd(Model model) {
        /*未做json转换*/
//        model.addAttribute("providerList", providerService.queryAllProviders());
        /*json转换*/
        model.addAttribute("providerList", JSON.toJSONString(providerService.queryAllProviders()));
        return "providerAdd";
    }

    //新增供应商
    @ResponseBody
    @RequestMapping("/providerAdd")
    public int providerAdd(Provider provider, HttpSession session) {
        User user = (User) session.getAttribute("user");
        provider.setCreatedBy(user.getId());
        provider.setCreationDate(new Date());
        System.out.println("新增页面传入的provider---" + provider);
        return providerService.addProvider(provider);
    }

    //查询供应商编码是否存在
    @ResponseBody
    @RequestMapping("/queryProCode")
    public boolean queryProCode(String proCode) {
        Provider provider = providerService.queryProviderByProCode(proCode);
        if (provider != null) {
            System.out.println("该供应商编码存在！");
            return true;
        } else {
            System.out.println("该供应商编码不存在！");
            return false;
        }
    }


}
